<?php
namespace app\applet\controller;

use app\applet\model\HousingInformation;
use app\applet\model\Shops;
use app\manage\model\AdminUser;
use think\Controller;
use think\Request;
use app\applet\model\OrderHouse as m_OrderHouse;
use think\Validate;

class OrderHouse extends Controller {
    public function __construct(Request $request = null)
    {
//        parent::__construct($request);
    }

    /**
     * 处理预约看房
     */
    public function DealOrderHouse() {
        $input = input();
        $validate = new Validate([
            ['id', 'require', 'id不能为空'],
            ['user_id', 'require', '用户id不能为空'],
            ['page', 'require', '页码不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }

        $id = $input['id'];
        $page = $input['page'];
        $user_id = $input['user_id'];
        $data['admin_status'] = 2;
        $data['deal_time'] = time();

        $model = new m_OrderHouse();
        $a = $data;
        $data_tmp = $model->getOne2($user_id, $page);

        if (!empty($data_tmp)) {
            $data = $data_tmp[0];
            if ($data['type'] == 1) {
                $model = new HousingInformation();
            } else {
                $model = new Shops();
            }
            $detail = $model->detail($data['house_id']);
            if ($data['type'] == 1) {
                $data['room_number'] = $detail['h_tung_num'] . '-' . $detail['h_unit_num'] . '-' . $detail['h_room_number'];
            } else {
                $data['room_number'] = $detail['s_tung_num'] . '-' . $detail['s_unit_num'] . '-' . $detail['s_room_number'];
            }
            $data['create_time'] = date("Y-m-d H:i", $data['create_time']);
            $data['deal_time'] = date("Y-m-d H:i", $data['deal_time']);
        } else {
            $data = null;
        }

        $model = new m_OrderHouse();
        $model->m_update($id, $a);
        returnJson(200,"成功", $data)->send();die;
    }

    /**
     * 预约看房列表
     */
    public function OrderHouse() {
        $input = input();
        $validate = new Validate([
//            ['type', 'require', '不能为空'],
            ['page', 'require', '不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }

        $admin_status = input()['admin_status'];
        $page = input()['page'];
        $admin_id = $input['admin_id'];
        $model = new AdminUser();
        $admin_id = $model->findPlatId($admin_id);

        $model = new m_OrderHouse();
        $data = $model->OrderListAdmin($page, $admin_status, $admin_id);

        if ((($page-1) * get_list_rows() + count($data['data'])) >= $data['count']) {
            $data['no_more'] = true;
        } else {
            $data['no_more'] = false;
        }

        foreach ($data['data'] as $k => &$value) {
            if ($value['type'] == 1) {
                $model = new HousingInformation();
            } else {
                $model = new Shops();
            }
            $detail = $model->detail($value['house_id']);
            if ($value['type'] == 1) {
                $value['room_number'] = $detail['h_tung_num'] . '-' . $detail['h_unit_num'] . '-' . $detail['h_room_number'];
            } else {
                $value['room_number'] = $detail['s_tung_num'] . '-' . $detail['s_unit_num'] . '-' . $detail['s_room_number'];
            }
            $value['create_time'] = date("Y-m-d H:i", $value['create_time']);
            $value['deal_time'] = date("Y-m-d H:i", $value['deal_time']);
        }

        returnJson(200,"成功", $data)->send();die;
    }

    /**
     * 待办事项
     */
    public function Todo() {
        $admin_id = input()['admin_id'];

        $model = new AdminUser();
        $admin_id = $model->findPlatId($admin_id);

        $model = new m_OrderHouse();
        $count = $model->getTodo($admin_id);
        $data[] = $count;
        returnJson(200,"成功", $count)->send();die;
    }
}